package com.apartment.controller.house;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.apartment.entity.House;
import com.apartment.service.house.HouseService;
import com.apartment.util.PageVo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/house")
public class HouseController {

    @Autowired
    private HouseService houseService;

    @RequestMapping("/findAll")
    public ModelAndView findAll() {
        ModelAndView mv = new ModelAndView();

        List<House> list = houseService.findAll();
        JSONArray array = JSONArray.parseArray(JSON.toJSONString(list));
        mv.addObject("array", array);
        mv.addObject("houses", list);
        mv.setViewName("/house/show");
        return mv;
    }

    @RequestMapping("/findAllVue")
    public PageVo queryList(Integer page, Integer limit) {
        /**
         * 这些代码应该写在service层
         */
        PageVo houseVo = houseService.finAllPage(2, 2);
        return houseVo;
    }

    @RequestMapping(value = "/list")
    public List<House> findAllVue() {
        List<House> list = houseService.findAll();
        return list;
    }
}
